<?php
/**
 * Created by PhpStorm.
 * User: WD-QD-PHP-DEV-YU
 * Date: 2017/5/2
 * Time: 10:28
 */
$this->title = '用户管理';
use yii\widgets\LinkPager;
use yii\helpers\Html;
use yii\helpers\Url;
?>
<?php $this->beginBlock('box-tools');?>
    <?=Html::a('增加',['/admin/user-admin/signup'],['class'=>'btn btn-sm btn-primary'])?>
<?php $this->endBlock();?>

<table id="example2" class="table table-bordered table-hover">
    <thead>
    <tr>
        <th>UID</th>
        <th>Username</th>
        <th>创建时间</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
        <?php foreach ($model as $user):?>
        <tr>
            <td><?=$user->id?></td>
            <td><?=$user->username?></td>
            <td><?=\Yii::$app->formatter->asDate($user->created_at,'yyyy-MM-dd')?></td>
            <td><?=$user->status==10?'正常':'禁用'?></td>
            <td>
                <div class="btn-group">
                    <?=Html::a('修改密码','javascript:;',['class'=>'btn btn-sm btn-success', 'onclick'=>"changePassword(".$user->id.")"])?>
                    <?=Html::a(' 禁用','javascript:;',['class'=>'btn btn-sm btn-danger','onclick'=>"deleteUser(".$user->id.")"])?>
                </div>
            </td>
        </tr>
        <?php endforeach;?>
    </tbody>
</table>


<?=LinkPager::widget(
    ['pagination'=>$pages]
)?>

<?php

$url = Url::to(['delete'],true);
$changePassUrl = Url::to(['change-password'],true);
$js = <<<JS
    function deleteUser(uid){
        if(!confirm('确认要执行该操作吗?')){
                return false;
            }
         $.ajax({
            url: '$url',
            type: 'post',
            data: {uid:uid},
            success: function (data) {
                if(data.status == 1){
                    location.reload();
                }else{
                    layer.msg(data.msg);
                }
            }
        });
    }
    
    function changePassword(uid) {
      layer.prompt(
                {title: '请输入新密码'},
                function(val){
                    layer.closeAll();
                    $.post(
                        "$changePassUrl",
                        {uid:uid,password:val},
                        function(data){
                            if (data.status==1) {
                                location.reload();
                            }else{
                               layer.msg(data.msg);
                            }
                        }

                    );
            });
    }
JS;
$this->registerJs($js,3);
?>



